const axios = require('axios');

// 测试配置
const BASE_URL = 'http://localhost:3000';

// 测试用例
const testCases = [
  {
    name: '测试飞书文档URL',
    url: 'https://bytedance.feishu.cn/docs/doccnAbCdEfGhIjKlMnOpQrStUvWxYz'
  },
  {
    name: '测试普通网页URL',
    url: 'https://httpbin.org/html'
  },
  {
    name: '测试无效URL',
    url: 'invalid-url'
  }
];

// 测试函数
async function testAPI() {
  console.log('开始API测试...\n');
  
  // 测试健康检查
  try {
    console.log('1. 测试健康检查端点');
    const healthResponse = await axios.get(`${BASE_URL}/health`);
    console.log('✅ 健康检查通过:', healthResponse.data);
  } catch (error) {
    console.log('❌ 健康检查失败:', error.message);
    return;
  }
  
  console.log('\n2. 测试内容提取端点');
  
  // 测试各种URL
  for (const testCase of testCases) {
    console.log(`\n测试: ${testCase.name}`);
    console.log(`URL: ${testCase.url}`);
    
    try {
      const response = await axios.post(`${BASE_URL}/extract`, {
        url: testCase.url
      });
      
      console.log('✅ 请求成功');
      console.log('响应类型:', response.data.type);
      console.log('内容长度:', response.data.content ? response.data.content.length : 0);
      
      if (response.data.type === 'feishu_markdown') {
        console.log('Markdown内容预览:', response.data.content.substring(0, 100) + '...');
      } else if (response.data.type === 'webpage_html') {
        console.log('HTML内容预览:', response.data.content.substring(0, 100) + '...');
      }
    } catch (error) {
      if (error.response) {
        console.log('❌ 请求失败:', error.response.data.error);
        console.log('状态码:', error.response.status);
      } else {
        console.log('❌ 网络错误:', error.message);
      }
    }
  }
  
  console.log('\n测试完成!');
}

// 运行测试
if (require.main === module) {
  testAPI().catch(console.error);
}

module.exports = { testAPI };